import 'dart:async';
import 'player_engine.dart';
import 'models/player_state.dart';
import 'models/media_info.dart';
import 'models/player_config.dart';

class FigoPlayerController {
  final FigoPlayerEngine _engine;
  final PlayerConfig _config;
  
  // 状态管理
  PlayerState _state = PlayerState.idle;
  double _currentTime = 0.0;
  double _duration = 0.0;
  MediaInfo? _mediaInfo;
  
  // 流控制器
  final StreamController<PlayerState> _stateController = StreamController.broadcast();
  final StreamController<double> _timeController = StreamController.broadcast();
  final StreamController<MediaInfo> _mediaInfoController = StreamController.broadcast();
  final StreamController<String> _errorController = StreamController.broadcast();
  
  FigoPlayerController({PlayerConfig? config}) 
      : _engine = FigoPlayerEngine(),
        _config = config ?? PlayerConfig.defaultConfig() {
    _setupEngineListeners();
  }
  
  void _setupEngineListeners() {
    _engine.stateStream.listen((state) {
      _state = state;
      _stateController.add(state);
    });
    
    _engine.timeStream.listen((time) {
      _currentTime = time;
      _timeController.add(time);
    });
    
    _engine.mediaInfoStream.listen((info) {
      _mediaInfo = info;
      _mediaInfoController.add(info);
    });
    
    _engine.errorStream.listen((error) {
      _errorController.add(error);
    });
  }
  
  // 基本控制
  Future<bool> loadMedia(String filePath) async {
    return await _engine.loadMedia(filePath, config: _config);
  }
  
  Future<bool> play() async {
    return await _engine.play();
  }
  
  Future<bool> pause() async {
    return await _engine.pause();
  }
  
  Future<bool> stop() async {
    return await _engine.stop();
  }
  
  Future<bool> seek(double timeSeconds) async {
    return await _engine.seek(timeSeconds);
  }
  
  Future<void> setVolume(double volume) async {
    await _engine.setVolume(volume);
  }
  
  // 移动端优化
  Future<void> setHardwareDecode(bool enable) async {
    await _engine.setHardwareDecode(enable);
  }
  
  Future<void> setBatteryOptimization(bool enable) async {
    await _engine.setBatteryOptimization(enable);
  }
  
  Future<void> setMemoryLimit(int mbLimit) async {
    await _engine.setMemoryLimit(mbLimit);
  }
  
  // 状态查询
  PlayerState get state => _state;
  double get currentTime => _currentTime;
  double get duration => _duration;
  MediaInfo? get mediaInfo => _mediaInfo;
  bool get isPlaying => _state == PlayerState.playing;
  bool get isPaused => _state == PlayerState.paused;
  bool get isStopped => _state == PlayerState.stopped;
  
  // 流监听
  Stream<PlayerState> get stateStream => _stateController.stream;
  Stream<double> get timeStream => _timeController.stream;
  Stream<MediaInfo> get mediaInfoStream => _mediaInfoController.stream;
  Stream<String> get errorStream => _errorController.stream;
  
  void dispose() {
    _engine.dispose();
    _stateController.close();
    _timeController.close();
    _mediaInfoController.close();
    _errorController.close();
  }
}
